﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GameSchool.Models.Interfaces;

namespace GameSchool.Models.Repositories
{
    /// <summary>
    /// The NewsRepository connects the controller to the NewsItem table in the database, providing basic CRUD functions.
    /// Avaliable functions are:
    /// 
    /// GetNews() - returns NewsItems ordered by date ascending.
    /// GetNewsByID()
    /// AddNews()
    /// DeleteNews()
    /// Save()
    /// </summary>

    public class NewsRepository : INewsRepository
    {
        GameSchoolDBDataContext db = new GameSchoolDBDataContext();

        //Fetches all News and returns them in descending order by the publishing date
        public IQueryable<NewsItem> GetNews()
        {

            var dateOrdered = from n in db.NewsItems
                              orderby n.DatePub descending
                              select n;

            return dateOrdered;

        }

        //Fetches a single News Item by it's ID
        public NewsItem GetNewsByID(int? nID)
        {
            if(nID == null)
                throw new ArgumentNullException("GetNewsByID - null passed as parameter");

            var nItem = (from news in db.NewsItems
                         where news.ID == nID
                         select news).SingleOrDefault();

            return nItem;
        }

        //Fetches all News in a certain Course by CourseID
        public IQueryable<NewsItem> GetNewsByCourseID(int? cID)
        {
            if (cID == null)
                throw new ArgumentNullException("GetNewsByID - null passed as parameter");

            var cItem = from news in db.NewsItems
                        where news.CourseID == cID
                        orderby news.DatePub descending
                        select news;

            return cItem;
        }

        public void AddNews(NewsItem n)
        {
            if (n.Title.Length == 0 || n.Content.Length == 0)
                throw new Exception("Missing title or content!");

            n.DatePub = DateTime.Now;
            db.NewsItems.InsertOnSubmit(n);
            db.SubmitChanges();
        }

        //Edit's a certain NewsItem by an ID
        public void EditNews(NewsItem n)
        {
            var original = GetNewsByID(n.ID);
           
            original.Title = n.Title;
            original.DatePub = DateTime.Now;
            original.Course = n.Course;
            original.Author = n.Author;
            original.Content = n.Content;

            db.SubmitChanges();
        }

        //Deletes a NewsItem by it's ID
        public void DeleteNews(int? nID)
        {
            if (nID == null)
                throw new ArgumentNullException("DeleteNews - null passed as parameter");

            var n = (from news in db.NewsItems
                     where news.ID == nID
                     select news).SingleOrDefault();

            db.NewsItems.DeleteOnSubmit(n);
        }
        
       public void Save()
        {
            db.SubmitChanges();
        }

    }
}